﻿using WH.Model.OrgContext;
using WH.Model.Systems;
using WH.Repositories.BaseRepositories;
using WH.Repositories.IRepositories.ISystem;
using SqlSugar;

namespace WH.Repositories.Repositories.System
{
    public class OrganizationRepository : BaseRepository<OrganizationEntity>, IOrganizationRepository
    {
        private readonly IKeyValueRepository _keyValueRepository;
        public OrganizationRepository(ISqlSugarClient dbContext, OrganizationContext organizationContext, IKeyValueRepository keyValueRepository) : base(dbContext, organizationContext)
        {
            _keyValueRepository = keyValueRepository;
        }

        protected override ISugarQueryable<OrganizationEntity> Query
        {
            get
            {
                var keyValues = _keyValueRepository.FindAllAsync(x=>x.IsEnable, true).Result;

                var query = DbContext.Queryable<OrganizationEntity>().Mapper(org =>
                {
                    org.OrgType = keyValues.Where(x => x.Code == org.TypeCode).FirstOrDefault();
                });

                return query;
            }
        }
    }
}
